package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.Music;
import com.baomidou.ant.service.IMusicService;
import com.baomidou.ant.utils.VoUtilsTool;
import com.baomidou.ant.vo.Request.RequestSaveMusicVo;
import com.baomidou.ant.vo.Request.RequestUpdateMusicVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    private IMusicService musicService;

    //更新音乐信息
    @PostMapping("/updateMusic")
    public Result updateMusic(@RequestBody RequestUpdateMusicVo vo){
        Music music=musicService.getById(vo.getMusicId());
        //修改信息并保存
        BeanUtils.copyProperties(vo,music);
        if (musicService.updateById(music)) {
            return Result.getSuccess().setMsg("修改成功");
        } else {
            return Result.getSuccess().setMsg("修改失败");
        }
    }


    //添加新音乐
    @PostMapping("/saveMusic")
    public Result saveMusic(@RequestBody RequestSaveMusicVo vo){
        //判断
        if (VoUtilsTool.checkObjFieldIsNull(vo)){
            return Result.getFailure().setMsg("不能有空项");
        }
        Music music=new Music();
        BeanUtils.copyProperties(vo,music);
        if(musicService.save(music)){
            return Result.getSuccess().setMsg("添加成功");
        }else{
            return Result.getFailure().setMsg("添加失败");
        }
    }
}
